<?php
/**
 * @copyright meila.com
 * @author wangtao5@
 * @name 发布接口
 * @param
 *         $nick = 作用
 *
 *
 */
include('../../__global.php');
class aj_albumhottopic extends ml_controller
{
    private $_top_n = 10;
    private $_top = 3;
    private $_title;
    private $_join_cnt=0;
    /**
     * 初始化参数
     */
    function initParam(){}
    
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam(){}
    
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main(){
        $album_array = ml_tool_getdata::lbJsonData('albumhottopic', 'albumfeed');
        if(is_array($album_array)){
            $rand_key = array_rand($album_array);
            $data = $album_array[$rand_key];
            $this->_title = $data['title'];
            $result = $this->getAlbumfeed($data['album_id']);
            if($result){
                $data['feedlist'] = $result;
                $data['join_cnt'] = $this->_join_cnt;
            }else{
                $data['feed_list'] = null;
                $data['join_cnt'] = 0;
            }
        }else{
            $data = array();
        }
        $this->api_output(ML_RCODE_SUCC, $data);
    }
    
    function getAlbumfeed($album_id){
        $oRdsAlbum = new ml_model_album_rdsAlbum();
        $this->_join_cnt = $oRdsAlbum->get_users_count($album_id);
        if($this->_join_cnt <= 0)
            return false;
        
        $oAlbumFeed = new ml_model_album_dbAlbumfeed();
        $rs = $oAlbumFeed->get_list_by_album_id($album_id, 0, $this->_top_n, 'reply_cnt desc');
        
        if(!$rs)
            return false;
        
        $list = $oAlbumFeed->get_data();
        $uids = array();
        $active_all = array();
        foreach($list as $key=>$album){
            $uids[] = $album['uid'];
            $active = ml_tool_active::findAllActive($album['content']);
            if($active){
                $active_all = array_merge($active_all, $active);
                $list[$key]['active'] = $active;
            }
        }
        if(count($active_all) > 0){
            $rs = $oRdsAlbum->exists_album_by_titles($active_all);
            if(!$rs){
                $is_active = false;
            }else{
                $is_active = true;
                $active_all = $rs;
            }
        }else{
            $is_active = false;
        }
            
        $uids = array_unique($uids);
        $oRdsHash = new ml_model_rdsHash();
        $rs = $oRdsHash->getNicksByUids($uids);
        if($rs)
            $users = $oRdsHash->get_data();
        else
            $users = array();
        
        $show_cnt = 0;
        $show_array = array();
        foreach($list as $key=>$album){
            $album['portrait'] = ml_tool_picid::uid2portrait($album['uid']);
            $album['nickname'] = $users[$album['uid']];
            //$album['content'] = ml_tool_formatContent::format_content($album['content']);
            if($is_active && $album['active']){
                $active = array_intersect($album['active'], $active_all);
                if(count($active)>0){
                    $content = trim(ml_tool_active::filter_active($album['content'], $active));
                    if(empty($content))
                        continue;
                    $album['content'] = $content;
                    $show_array[$show_cnt] = $album;
                    $show_cnt++;
                    if($show_cnt>=$this->_top){
                        break;
                    }
                        
                }
            }else{
                $show_array[$show_cnt] = $album;
                $show_cnt++;
                if($show_cnt>=$this->_top){
                    break;
                }
            }
        }
        return $show_array;
    }
    
    
}

new aj_albumhottopic();
